﻿namespace Pre.CRM.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;

    using Pre.CRM.Models;

    using VIT.BusinessLogicLayer.CRM;
    using VIT.DataSource;

    [Authorize]
    public class CustomerController : Controller
    {
        private readonly AgentBLL _agentBLL;
        private readonly CustomerBLL _customerBLL;

        public CustomerController()
        {
            this._agentBLL = new AgentBLL();
            this._customerBLL = new CustomerBLL();
        }

        public ActionResult Index(string agent)
        {
            var status = Request["status"];
            if (string.IsNullOrEmpty(status)) status = "1,2,3,4,5,6";
            var statuss = status.Split(',').Select(id => Convert.ToInt32(id)).ToList();

            var model = new CustomersModel();
            model.Agent = agent;
            model.Status = statuss;
            model.Customers = this._customerBLL.GetAllCustomerByAgent(model.Agent, model.Status);
            model.WebStatus = DataSource.WebStatusCollection;
            return View(model);
        }

        public ActionResult Detail(string agent, string customer)
        {
            var model = new CustomerDetailModel();
            model.Id = customer;
            model.Agent = agent;
            model.Customer = this._customerBLL.GetCustomerByDomain(agent, customer);

            return View(model);
        }

        public ActionResult Contract(string agent, string customer)
        {
            var model = new CustomerContractModel();
            model.Id = customer;
            model.Agent = agent;
            model.Domains = this._customerBLL.GetDomainsByCustomer(agent, customer);
            model.Templates = this._customerBLL.GetTemplatesByCustomer(agent, customer);
            model.Contracts = this._customerBLL.GetContractByDomain(customer);
            model.Services = DataSource.CRMServiceCollection;
            return View(model);
        }

        [HttpPost]
        public ActionResult Contract(string agent, string customer, string action, string domain, string template, string service, string price, string repeat)
        {
            switch (action)
            {
                case "DELETEDOMAIN":
                    this._customerBLL.DeleteDomain(customer, agent, customer, domain, User.Identity.Name);
                    break;
                case "ADDDOMAIN":
                    this._customerBLL.AddDomain(customer, agent, customer, domain, User.Identity.Name);
                    break;
                case "DELETETEMPLATE": 
                    this._customerBLL.DeleteTemplate(customer, agent, template, User.Identity.Name);
                    break;
                case "ADDTEMPLATE": 
                    this._customerBLL.AddTemplate(customer, agent, template, User.Identity.Name);
                    break;
                case "DELETESERVICE":
                    this._customerBLL.DeleteService(customer, Convert.ToInt32(service), User.Identity.Name);
                    break;
                case "ADDSERVICE":
                    this._customerBLL.AddService(customer, Convert.ToInt32(service), Convert.ToDecimal(price), repeat == "on" ? true : false, User.Identity.Name);
                    break;
            }

            var model = new CustomerContractModel();
            model.Id = customer;
            model.Agent = agent;
            model.Domains = this._customerBLL.GetDomainsByCustomer(agent, customer);
            model.Templates = this._customerBLL.GetTemplatesByCustomer(agent, customer);
            model.Contracts = this._customerBLL.GetContractByDomain(customer);
            model.Services = DataSource.CRMServiceCollection;
            return View(model);
        }

        public ActionResult CreateKey(string customer, string agent)
        {
            var objCustomer = this._customerBLL.GetCustomerByDomain(agent, customer);
            var objAgent = this._agentBLL.GetAgentByDomain(agent);
            var model = new CustomersKeyModel();
            model.Id = customer;
            model.Domains = objCustomer.Domains;
            model.Keys = objCustomer.Key;
            model.EmailCustomer = objCustomer.CompanyEmail;
            model.EmailAgent = objAgent.AgentEmail;
            return View(model);
        }

        [HttpPost]
        public ActionResult CreateKey(string customer, string agent, string action, string year, string keys, string email)
        {
            var objCustomer = this._customerBLL.GetCustomerByDomain(agent, customer);
            var objAgent = this._agentBLL.GetAgentByDomain(agent);
            var model = new CustomersKeyModel();
            model.Id = customer;
            model.Domains = objCustomer.Domains;
            model.Years = Convert.ToInt32(year);
            model.Keys = keys;
            model.EmailCustomer = objCustomer.CompanyEmail;
            model.EmailAgent = objAgent.AgentEmail;

            switch (action)
            {
                case "CREATEKEY": model.Keys = this._customerBLL.CreateKey(agent, customer, model.Years, model.Domains, User.Identity.Name);
                    break;
                case "UPDATEKEY": this._customerBLL.UpdateKey(agent, customer, model.Years, model.Keys, User.Identity.Name);
                    break;
                case "SENDMAIL": this._customerBLL.SendKey(email, model.Keys, model.Years, customer, User.Identity.Name);
                    break;
            }

            return View(model);
        }

        public ActionResult Contact(string customer)
        {
            var model = new CustomerContactModel();
            model.Id = customer;
            model.ContactMethods = DataSource.CRMContactMethodCollection;
            model.ContactReasons = DataSource.CRMContactReasonCollection;
            model.Contacts = this._customerBLL.GetContactByDomain(model.Id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Contact(string customer, string method, string reason, string note)
        {
            this._customerBLL.InsertContactIntoCustomer(customer, method, Convert.ToInt32(reason), note, User.Identity.Name);
            return this.Contact(customer);
        }
    }
}
